package ee.ttu.agent.smartHouse.gui;

import jade.gui.GuiEvent;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ee.ttu.agent.smartHouse.agent.FacilitatorAgent;
import ee.ttu.agent.smartHouse.util.CustomEventTypes;

public class FacilitatorAgentGui extends JFrame implements ActionListener {
	private static final long serialVersionUID = 4380741562450730024L;
	
	private JButton doorBell;
	private StatusPanel statusPanel;
	private JTextField authCodeField;
	
	private FacilitatorAgent myAgent; // Reference to the agent class
	
	public FacilitatorAgentGui(FacilitatorAgent faclilitatorAgent) {
		this.setSize(300, 200);
//		this.setTitle("Gate Keeper Panel");
		this.setTitle(faclilitatorAgent.getName());
		
		Container content = this.getContentPane();
		content.setBackground(Color.white);
		content.setLayout(new FlowLayout());
		
		JPanel p = new JPanel(new BorderLayout());
		p.setLayout(new GridLayout(2, 2));
		p.add(new JLabel("Authentication Code:"));
		
		authCodeField = new JTextField(15);
		p.add(authCodeField);
		doorBell = new JButton("Open the door");
		
		statusPanel = new StatusPanel();
		statusPanel.closed();
		doorBell.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				statusPanel.onHold();
				
				GuiEvent ge = new GuiEvent(this, CustomEventTypes.ENTRANCE_ATTEMPT);
				myAgent.postGuiEvent(ge);
			}
		});
		
		content.add(p);
		content.add(doorBell);
		content.add(statusPanel);
		
		this.addWindowListener(new java.awt.event.WindowAdapter() {
	        public void windowClosing(WindowEvent winEvt) {
	        	GuiEvent ge = new GuiEvent(this, CustomEventTypes.KILL_AGENT);
				myAgent.postGuiEvent(ge);
	        }
	    });
		
		myAgent = faclilitatorAgent;
	}
	
	public void openDor(){
		statusPanel.opened();
	}
	
	public void closeDor(){
		statusPanel.closed();
	}
	
	public void onHoldDor(){
		statusPanel.onHold();
	}
	
//	public JTextField getAuthCodeField() {
//		return authCodeField;
//	}
	
	public String getAuthCode() {
		return authCodeField.getText();
	}
	
	public void actionPerformed(ActionEvent e) {
	
	}
	
	@SuppressWarnings("serial")
	private static class StatusPanel extends JPanel{
		
		private CirclePanel green;
		private CirclePanel yellow;
		private CirclePanel red;

		public StatusPanel() {
			setBackground(Color.WHITE);
			
			green = new CirclePanel(Color.GREEN);
			yellow = new CirclePanel(Color.YELLOW);
			red = new CirclePanel(Color.RED);
			
			add(green);
			add(yellow);
			add(red);
		}
		
		public void onHold(){
			green.turnOff();
			yellow.turnOn();
			red.turnOff();
		}
		
		public void closed(){
			green.turnOff();
			yellow.turnOff();
			red.turnOn();
		}
		
		public void opened(){
			green.turnOn();
			yellow.turnOff();
			red.turnOff();
		}
	}
	
	 @SuppressWarnings("serial")
	static class CirclePanel extends JPanel {
		private boolean isOn = false;
		private Color color;
		public CirclePanel(Color color) {
			this.color = color;
			setPreferredSize(new Dimension(12, 12));
			setBackground(Color.WHITE);
		}
		
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			drawCircle(g, 5, 5, 5); // center (30,30) r=20
		}
		
		public void drawCircle(Graphics cg, int xCenter, int yCenter, int r) {
			cg.setColor(color);
			cg.drawOval(xCenter-r, yCenter-r, 2*r, 2*r);
	
	        cg.setColor(isOn ? color : Color.WHITE);
	        cg.fillOval(xCenter-r, yCenter-r, 2*r, 2*r);
		}
		
		public void turnOn(){
			isOn = true;
			repaint();
		}
		
		public void turnOff(){
			isOn = false;
			repaint();
		}
	}
}
